package com.atguigu.gmall.pay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lx
 * @Time: 15:38
 * @Description:
 */
public class AlipayDemo {


    //连接Alipay   SDK
    public static void main(String[] args) throws Exception{
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",//请求地址
                "2016102100732915",//商户的ID
                "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCDgBoNOqwmV7QGjJnc0hcZGixIR9EbSQWZ7nvHQYFV88/AHejn+jriO5+jGWGFmOw0T8NNp6Mh9GBgE0BEEz6kZocf7KB06K0PsiUP2239wfkFJqg0l8m3zM+H4d+OK7Z1egimKnS3yxT2phsiXdOvGA8K9Vn4V9Ogm85rlRV64Qbz94mUXNola35n7dFO/mMmeUTU5RYfs/L2n0K3Hb410KQ3i082aXdUnYBUKq/meCmu3vEY1yuOd53cshCT9KSsEQxrpbwAJJXeFvDWJBm2zYZ9D3YHBwPjOhKbAWgiy4p/QJOm8j4N14sjvRQ8bf3vRarQ9sM1p7DduIMhrCczAgMBAAECggEAbmfvd4v7XXXlQFpsFGqP2hOPemBRwIbenQ8N/Y2R+YhqtPJnydzd83P75AQ7TN/H8WPGpjxJSwjSodeH7Z/6yC0M5lDVp1JzeuonE9k30AY7/YcnUoDea+MZmwyZldQ9rQR4noqm1Gk9gJiVz5TXVfrtJlY447lZhU9LUCgrkK6IK+3aeIWeCNGf4FVnf9WBbtN3iTQDnG/cDr3esS6cfhAdrUqByXkvJYvWPmOxoyinD9dQPnJ9ptRpxrgl5ANtUJETOYUB9ipu2wnJmIzkaHdy7UK8LqreSVkcLOYj0B+BXtmX6CjXDdDZbE7oONSFyU1JK8Co7u0ueaCckMoLAQKBgQDMkj7GlAuHaBtU+ILjWjfJY3uQ6ihZVbA0z5yFuJZYORuGBr6JeDbfDVgffkycbb5jCNhsDNMsaqqLx7cxE2xR8SkLqXgZPqrhMIYpyIb9YMZFprOR6SO00++f3WVkaM2FhtUdROg40KOp8tLkFFH4VJzLqDBOmywhJ3eEspYFGwKBgQCkjy07Hj4DPp4PckvZ+1coXnlRhb4XmqeL1liBXz1ER/t72ThXxrUMaT01nMzOSOMeAHpZLMGwQ6bhRZSboWRJtEnfzn8+SKz4w+6M+pnshT4jHSASVm4BUxJLaINyKq3qLfc60pbddP7+T3iSO6DBWkEqOjkSWCDBoTEI7Gm/yQKBgQCijzwPNKmMqi+979SisZhPEhyo5Um2456n+KpaoHnoFUlH0agrOTnxOXtjW0upqpN1wxunkPFmBEUN2mA6QUL8QnTpbXugI+pKE/Mo1M5gHjywMG3VbrwO/7MVCCuABL/8cU58ebtHGpRTgkAPOg6BFUubkkUA0dHZbrFM41TPLQKBgQChxQan+UDpG6Nyd9/NOGIkE0ucMqf+uFBBDcQP2jp2Z3T3+UCEOZk9zC8aBzznBYwPsFSWWYNG7hqdljNTE0NtmUiONYetCPDR1HssKCcs9odxKW6f2EtoDewMKYAeuYoijnFeaCtbJWqPQjy7NNcBfOJWysnUGMKK1xVHOnFy0QKBgQCTP21TvuTx/RzBwWlSRDVpkeZDJphx/dMK7CQ9tpTvg2joXngKq5lbAH37jcfDab6/SXYAJuuCHCc9CyGezVHZQwyWteyKWcIl8vDM8QvHgmhI2vJLp5k5Ya1GUT4R9IyVUTw7FoqlfVFG7gBDKuLMc4kHFjMs9TwZD7Jn2vwnhQ==", //自己的私钥
                "json","UTF-8",//编码  格式
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv4FgjWsfvbXEkzs5T+z5r4X9m5Qh8OA0zlZA3T9yeCqlcRRqydMBfRsB4V9aHUH0KCV875mzF7Feh3VoDilcDpGtuKo5l7YRVnGSyQTq0pQjMFYi/wKGFGHAA37Wl2L/AMaGpT6CJncSqcAJssY61qmz/o9lw+hRq6KBfdktl4xm0d5f+uMgCLKqa6bYbFyG7vNZszGJ6yW2n6fFbZ0B4PgPXfrRFVG+OS204bi7sGDba6DuJPPuk8B/gkVoDkmKHNRnLXZi74jyZROlEXKr8JElZqpT6RODkKAmIRCGlQrAadUntgmymguRlN2K+TfSNIigY1FRekb/BudYhlbA5QIDAQAB",//阿里的公钥
                "RSA2");//加密  非对称加密
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        Map map = new HashMap<>();
        map.put("out_trade_no","123124324242");
        map.put("product_code","FAST_INSTANT_TRADE_PAY");
        map.put("total_amount","0.01");
        map.put("subject","哈哈");
        request.setBizContent(JSONObject.toJSONString(map));
        //发送
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
//获取需提交的form表单
        String submitFormData = response.getBody();
        System.out.println(submitFormData);
//客户端拿到submitFormData做表单提交


/*        <form name="punchout_form" method="post" action="https://openapi.alipaydev.com/gateway.do?charset=UTF-8&method=alipay.trade.page.pay&sign=Xp8vyCkIBo7CPLNJu%2FuGG1%2FJj78vyd1S4pzpxtaW1vyqLvUU8ZoU52u%2F1E1zeUux2jqw5osbxscyzAcyG%2F7OXmRCrqvhmBAuz0v5%2FdXYZXLsrs2GG%2BX0O0Zc4OZYhd3osGfQ88aWszqTxUjjKjHka848MsBGW0%2FgQuzRafrhTTtKUfrBUApYXbdJjQJGNINOZUtIfQl6gh6mkQxMG5yXLKwc49KWDVh30H0KijLv7C3UUDbCaH7bCXTEEVR6vve9%2FapbfU7DQCBGMvI8Qu96shZd90N4EUozD%2BypXgPDrMetaa52PjIuVzESTsNI7z0J0QUvwQfwx4qQc4NpFMJSkw%3D%3D&version=1.0&app_id=2016102100732915&sign_type=RSA2&timestamp=2020-12-05+16%3A04%3A22&alipay_sdk=alipay-sdk-java-4.8.73.ALL&format=json">
<input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;123124324242&quot;,&quot;total_amount&quot;:&quot;0.01&quot;,&quot;subject&quot;:&quot;哈哈&quot;,&quot;product_code&quot;:&quot;FAST_INSTANT_TRADE_PAY&quot;}">
<input type="submit" value="立即支付" style="display:none" >
</form>
<script>document.forms[0].submit();</script>*/

    }

}
